﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace AutoClick.v1
{
    public class Win32API
    {
        public const int MOD_ALT = 0x0001;
        public const int MOD_CONTROL = 0x0002;
        public const int MOD_SHIFT = 0x0004;
        public const int MOD_WIN = 0x0008;
        public const int WM_HOTKEY = 0x0312;
        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
        private const int MOUSEEVENTF_MIDDLEUP = 0x40;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        private const int MOUSEEVENTF_RIGHTUP = 0x0010;

        [DllImport("user32.dll")]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);

        [DllImport("user32.dll")]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
        [DllImport("user32.dll")]
        private static extern void mouse_event(int dwFlags, int dx, int dy,
        int dwData, int dwExtraInfo);

        private static (int, int, string) GetMouseActive(MouseKey mouseKey)
        {
            string key = "左键";
            int down = MOUSEEVENTF_LEFTDOWN;
            int up = MOUSEEVENTF_LEFTUP;
            switch (mouseKey)
            {
                case MouseKey.Left:
                    break;
                case MouseKey.Middle:
                    down = MOUSEEVENTF_MIDDLEDOWN;
                    up = MOUSEEVENTF_MIDDLEUP;
                    key = "中键";
                    break;
                case MouseKey.Right:
                    down = MOUSEEVENTF_RIGHTDOWN;
                    up = MOUSEEVENTF_RIGHTUP;
                    key = "右键";
                    break;
                default:
                    break;
            }
            return (down, up, key);
        }

        public static void MouseClick(MouseKey mouseKey)
        {
            var (down, up, key) = GetMouseActive(mouseKey);
            Debug.WriteLine($"单击，x:{Cursor.Position.X}, y:{Cursor.Position.Y}，key:{key}");
            // 模拟鼠标按下左键
            mouse_event(down, Cursor.Position.X, Cursor.Position.Y, 0, 0);
            // 等待一段时间
            Thread.Sleep(Config.ConfigModel.Duration);
            // 模拟鼠标抬起左键
            mouse_event(up, Cursor.Position.X, Cursor.Position.Y, 0, 0);
        }

        public static void MouseDoubleClick(MouseKey mouseKey)
        {
            var (down, up, key) = GetMouseActive(mouseKey);
            Debug.WriteLine($"双击，x:{Cursor.Position.X}, y:{Cursor.Position.Y}，key:{key}");
            // 模拟鼠标按下左键
            mouse_event(down, Cursor.Position.X, Cursor.Position.Y, 0, 0);
            // 模拟鼠标抬起左键
            mouse_event(up, Cursor.Position.X, Cursor.Position.Y, 0, 0);
            // 等待一段时间
            Thread.Sleep(100);
            // 模拟鼠标按下左键
            mouse_event(down, Cursor.Position.X, Cursor.Position.Y, 0, 0);
            // 模拟鼠标抬起左键
            mouse_event(up, Cursor.Position.X, Cursor.Position.Y, 0, 0);
        }
    }
}
